package org.testtools.formatter;

public class StringFormatter {

  private static final String GT_ALT = "&gt;";

  private static final String GT = ">";

  private static final String LT_ALT = "&lt;";

  private static final String LT = "<";

  private static final String GT_LT_ALT = ">&lt;";

  private static final String GT_ALT_LT_ALT = "&gt;&lt;";

  private static final String GT_LT = "><";

  private static final String GT_SPACE_LT_ALT = ">[ \n\t]{1,}&lt;";

  private static final String GT_ALT_SPACE_LT_ALT = "&gt;[ \n\t\b]{1,}&lt;";

  private static final String GT_SPACE_LT = ">[ \n\t\b]{1,}<";

  private static final String SPACE = " ";

  private static final String XMP_END = "</xmp>";

  private static final String XMP_BEGIN = "<xmp>";

  private static final String GT_ALT_SPACE_LT = "&gt;[ \n\t\b]{1,}<";

  private static final String GT_ALT_LT = "&gt;<";

  public static String htmlWithTabulation(String original) {
    return XMP_BEGIN + original + XMP_END;
  }

  public static void printArrayWithBlank(String[] cmd) {
    System.out.println(getArrayWithBlank(cmd));
  }

  public static String getArrayWithBlank(String[] cmd) {
    final StringBuffer sb = new StringBuffer();

    for (int i = 0; i < cmd.length; i++) {
      sb.append(cmd[i]).append(SPACE);
    }

    return sb.toString();
  }

  public static String encodeXMLTags(final String str) {
    return str.replaceAll(LT, LT_ALT).replaceAll(GT, GT_ALT);
  }

  public static String trimXMLTags(final String str) {
    return str.replaceAll(GT_SPACE_LT, GT_LT)
              .replaceAll(GT_ALT_SPACE_LT_ALT, GT_ALT_LT_ALT)
              .replaceAll(GT_SPACE_LT_ALT, GT_LT_ALT)
              .replaceAll(GT_ALT_SPACE_LT, GT_ALT_LT);
  }
}
